package logic

import (
	"context"
	"hire/hirerpc/hirerpc"
	"hire/pkg"
	"strconv"

	"hire/api/internal/svc"
	"hire/api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type HireLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewHireLogic(ctx context.Context, svcCtx *svc.ServiceContext) *HireLogic {
	return &HireLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *HireLogic) Hire(req *types.HireRequest) (resp *types.Response, err error) {
	// todo: add your logic here and delete this line
	hire, err := l.svcCtx.HireRpc.Hire(l.ctx, &hirerpc.HireRequest{
		Mobile:   req.Mobile,
		Password: req.Password,
		Id:       req.Id,
		SmsCode:  req.SmsCode,
	})
	if err != nil {
		return &types.Response{
			Code:    500,
			Message: err.Error(),
			Data:    nil,
		}, nil
	}
	handler, err := pkg.TokenHandler(strconv.FormatInt(hire.HireId, 10))
	if err != nil {
		return &types.Response{
			Code:    500,
			Message: err.Error(),
			Data:    nil,
		}, nil
	}
	return &types.Response{
		Code:    200,
		Message: "登录成功",
		Data:    handler,
	}, nil
}
